/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * jifAdminCosteoDiario.java
 *
 * Created on 10-dic-2010, 12:18:02
 */

package Costos;


import administracion.delegate.GestionPedidosDelegate;
import administracion.helper.Utiles_MensajesDialogo;
import administracion.helper.VerificarDatos;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import org.jdesktop.beansbinding.AutoBinding;
import org.jdesktop.beansbinding.BindingGroup;
import org.jdesktop.beansbinding.ELProperty;
import org.jdesktop.swingbinding.JTableBinding;
import org.jdesktop.swingbinding.SwingBindings;
import suministros.ejb30.entity.CosteoDiario;


/**
 *
 * @author Luis_Angel
 */
public class jifAdminCosteoDiario extends javax.swing.JDialog {

    private List<CosteoDiario> lista;
    private JTableBinding jTableBinding;
    private org.jdesktop.beansbinding.BindingGroup bindingGroup;
    /** Creates new form jifAdminCosteoDiario */
    public jifAdminCosteoDiario(java.awt.Frame parent, boolean modal) {
         super(parent, modal);
        initComponents();
         try {
                    lista = GestionPedidosDelegate.getInstance().getCosteoDiario();

                } catch (Exception ex) {
                    ex.printStackTrace();
                    Logger.getLogger(getClass().getName()).log(Level.SEVERE, null, ex);
                }
        cargaCosteo();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jtCosteo = new javax.swing.JTable();
        jbtnActualizar = new javax.swing.JButton();
        jbtnSalir = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jftFechaInicio = new javax.swing.JFormattedTextField();
        jftFechaFin = new javax.swing.JFormattedTextField();
        jLabel2 = new javax.swing.JLabel();
        jbtnEliminar = new javax.swing.JButton();

        setTitle("Administrar Costeo Diario");

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(""));

        jtCosteo.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Fecha", "Suministro", "Proveedor", "Costo", "Descripcion", "Cantidad"
            }
        ));
        jScrollPane1.setViewportView(jtCosteo);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 613, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 291, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jbtnActualizar.setFont(new java.awt.Font("Arial", 0, 12)); // NOI18N
        jbtnActualizar.setText("Modificar");
        jbtnActualizar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnActualizarActionPerformed(evt);
            }
        });

        jbtnSalir.setFont(new java.awt.Font("Arial", 0, 12)); // NOI18N
        jbtnSalir.setText("Cancelar");
        jbtnSalir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnSalirActionPerformed(evt);
            }
        });

        jButton1.setText("Cargar");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jLabel1.setText("Fech. Inicio");

        try {
            jftFechaInicio.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("##/##/####")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }
        jftFechaInicio.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jftFechaInicioActionPerformed(evt);
            }
        });

        try {
            jftFechaFin.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("##/##/####")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }

        jLabel2.setText("Fech. Fin");

        jbtnEliminar.setFont(new java.awt.Font("Arial", 0, 12)); // NOI18N
        jbtnEliminar.setText("Eliminar");
        jbtnEliminar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnEliminarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(127, 127, 127)
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jftFechaInicio, javax.swing.GroupLayout.PREFERRED_SIZE, 88, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(32, 32, 32)
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jftFechaFin, javax.swing.GroupLayout.PREFERRED_SIZE, 88, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jButton1))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(174, 174, 174)
                        .addComponent(jbtnEliminar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jbtnActualizar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jbtnSalir)))
                .addContainerGap())
        );

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {jbtnActualizar, jbtnSalir});

        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(11, 11, 11)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jftFechaFin, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1)
                    .addComponent(jftFechaInicio, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 307, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jbtnSalir)
                    .addComponent(jbtnActualizar)
                    .addComponent(jbtnEliminar))
                .addGap(17, 17, 17))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jbtnActualizarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnActualizarActionPerformed
        int filaElegida = this.jtCosteo.getSelectedRow();

        if (filaElegida < 0) {
            Utiles_MensajesDialogo.mensajeError("Debe elegir un item!");
            this.jtCosteo.requestFocus();
            return;
        }

        try {
            CosteoDiario tcd = (CosteoDiario)this.lista.get(this.jtCosteo.convertRowIndexToModel(filaElegida));
            jdCosteoDiario admin = new jdCosteoDiario(JOptionPane.getFrameForComponent((JDialog)this),false);
            admin.setAdmin(tcd);
            admin.setLocationRelativeTo(this);
            admin.setVisible(true);
        } catch (Exception e) {
            e.printStackTrace();
            Utiles_MensajesDialogo.mensajeError("No se pudo Cargar el archivo de tipos de cambio\n" + e.getMessage());
        }
}//GEN-LAST:event_jbtnActualizarActionPerformed

     private void cargaCosteo(){
        this.bindingGroup = new BindingGroup();
               
            this.jTableBinding = SwingBindings.createJTableBinding(AutoBinding.UpdateStrategy.READ,lista , this.jtCosteo);

            JTableBinding.ColumnBinding columnBinding0 = this.jTableBinding.addColumnBinding(ELProperty.create("${idSuministro.descripcion}"));

            columnBinding0.setColumnName("Suministro");
            columnBinding0.setColumnClass(String.class);
            columnBinding0.setEditable(false);

            JTableBinding.ColumnBinding columnBinding0_1 = this.jTableBinding.addColumnBinding(ELProperty.create("${codProveedor.razonSocial}"));

            columnBinding0_1.setColumnName("Proveedor");
            columnBinding0_1.setColumnClass(String.class);
            columnBinding0_1.setEditable(false);

            JTableBinding.ColumnBinding columnBinding1= this.jTableBinding.addColumnBinding(ELProperty.create("${fecha}"));

            columnBinding1.setColumnName("Fecha");
            columnBinding1.setColumnClass(String.class);
            columnBinding1.setEditable(false);


            JTableBinding.ColumnBinding columnBinding2 = this.jTableBinding.addColumnBinding(ELProperty.create("${idElementoCosto.descElemento}"));

            columnBinding2.setColumnName("Elelento Costo");
            columnBinding2.setColumnClass(String.class);
            columnBinding2.setEditable(true);

            JTableBinding.ColumnBinding columnBinding3 = this.jTableBinding.addColumnBinding(ELProperty.create("${descripcion}"));

            columnBinding3.setColumnName("Descripcion");
            columnBinding3.setColumnClass(String.class);
            columnBinding3.setEditable(true);

            JTableBinding.ColumnBinding columnBinding4 = this.jTableBinding.addColumnBinding(ELProperty.create("${cantidad}"));

            columnBinding4.setColumnName("Cantidad");
            columnBinding4.setColumnClass(BigDecimal.class);
            columnBinding4.setEditable(true);

            JTableBinding.ColumnBinding columnBinding5 = this.jTableBinding.addColumnBinding(ELProperty.create("${montoDolares}"));

            columnBinding5.setColumnName("Monto Dolares");
            columnBinding5.setColumnClass(BigDecimal.class);
            columnBinding5.setEditable(true);

            /*JTableBinding.ColumnBinding columnBinding8 = this.jTableBinding.addColumnBinding(ELProperty.create("${dominical}"));

            columnBinding8.setColumnName("Dominical");
            columnBinding8.setColumnClass(Boolean.class);
            columnBinding8.setEditable(true);*/


            this.bindingGroup.addBinding(this.jTableBinding);
            this.bindingGroup.bind();


    }

    private void jbtnSalirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnSalirActionPerformed
        // TODO add your handling code here:
        this.dispose();
}//GEN-LAST:event_jbtnSalirActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
       try {
                     Date fecha_ini = new Date(VerificarDatos.verificaFecha(this.jftFechaInicio.getText()).getTime());
                     Date fecha_fin = new Date(VerificarDatos.verificaFecha(this.jftFechaFin.getText()).getTime());

                    lista = GestionPedidosDelegate.getInstance().getCosteoDiarioFechas(fecha_ini,fecha_fin);

                } catch (Exception ex) {
                    Logger.getLogger(getClass().getName()).log(Level.SEVERE, null, ex);
                }
        cargaCosteo();
        
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jftFechaInicioActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jftFechaInicioActionPerformed
       this.jftFechaFin.requestFocus();
    }//GEN-LAST:event_jftFechaInicioActionPerformed

    private void jbtnEliminarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnEliminarActionPerformed
        int filaElegida = this.jtCosteo.getSelectedRow();

        if (filaElegida < 0) {
            Utiles_MensajesDialogo.mensajeError("Debe elegir un item!");
            this.jtCosteo.requestFocus();
            return;
        }

        try {
            CosteoDiario tcd = (CosteoDiario)this.lista.get(this.jtCosteo.convertRowIndexToModel(filaElegida));
            GestionPedidosDelegate.getInstance().eliminaCosteoDiario(tcd.getIdCosteoDiario().intValue());
            Utiles_MensajesDialogo.mensajeExito();
            try {
                     Date fecha_ini = new Date(VerificarDatos.verificaFecha(this.jftFechaInicio.getText()).getTime());
                     Date fecha_fin = new Date(VerificarDatos.verificaFecha(this.jftFechaFin.getText()).getTime());

                    lista = GestionPedidosDelegate.getInstance().getCosteoDiarioFechas(fecha_ini,fecha_fin);

                } catch (Exception ex) {
                    Logger.getLogger(getClass().getName()).log(Level.SEVERE, null, ex);
                }
            cargaCosteo();
        } catch (Exception e) {
            e.printStackTrace();
            Utiles_MensajesDialogo.mensajeError("No se pudo Cargar el archivo de tipos de cambio\n" + e.getMessage());
        }
    }//GEN-LAST:event_jbtnEliminarActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton jbtnActualizar;
    private javax.swing.JButton jbtnEliminar;
    private javax.swing.JButton jbtnSalir;
    private javax.swing.JFormattedTextField jftFechaFin;
    private javax.swing.JFormattedTextField jftFechaInicio;
    private javax.swing.JTable jtCosteo;
    // End of variables declaration//GEN-END:variables

}
